
package org.soekrat.engine.socraton;

import java.util.ArrayList;
import java.util.List;

import org.japril.jutil.CharArrayPrintWriter;
import org.japril.jutil.SaxUtil;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public final class SocratonsXmlProtocolSaxHandler extends DefaultHandler
{
  private final StringBuilder m_sb=new StringBuilder();
  
  private Locator m_loc=null;
  
  private String m_method=null;
  
  private List<SocData> m_parsed=new ArrayList<SocData>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public SocratonsXmlProtocolSaxHandler(){}
  
  /**
   * <p>
   *   Gets method.
   * </p>
   * @return Method, {@code null} if non parsed.
   */
  public String getMethod(){ return m_method; }
  
  /**
   * <p>
   *   Get parse result.
   * </p>
   * @return Parse result.
   */
  public List<SocData> getParsed(){ return m_parsed; }
  
  @Override
  public String toString() 
  {
    CharArrayPrintWriter capw=new CharArrayPrintWriter();
    capw.println("method: "+m_method);
    for (SocData curr : m_parsed)
    {
      capw.println(curr);  
    }
    return capw.toString();
  }

  @Override
  public void characters(char[] chars, int start, int len) 
    throws SAXException 
  {
    m_sb.append(chars, start, len);
  }

  @Override
  public void endElement(String uri, String lName, String qName)
    throws SAXException 
  {
    String txt=m_sb.toString();
    txt=(txt==null) ? "" : txt.trim();
    
    String n=qName;
    if (n.equals("bison"))
    {
      if (txt.length()>0) m_currBisons.add(txt);
    }
    
    m_sb.setLength(0);
  }

  @Override
  public void setDocumentLocator(Locator loc){ m_loc=loc; }

  @Override
  public void startElement(String uri, String lName, String qName,
    Attributes attrs) throws SAXException
  {
    String n=qName;
         if (n.equals("socratons")) handleSocratonsStart(attrs);
    else if (n.equals("socraton"))  handleSocratonStart(attrs);
    else if (n.equals("context"))   handleContextStart(attrs);
    else if (n.equals("bison"))     ;
  }
  
  private void handleSocratonsStart(Attributes attrs) throws SAXException
  {
    m_method=attrs.getValue("method");
  }
  
  private SocData m_curr=null;
  private List<String> m_currBisons=null;
  
  private void handleSocratonStart(Attributes attrs) throws SAXException
  {
    String id=SaxUtil.getMandatoryAttr("id", attrs, m_loc);
    String who=attrs.getValue("who");
    String bison=attrs.getValue("bison");
    String context=attrs.getValue("context");
    
    m_curr=new SocData(id, who);
    m_parsed.add(m_curr);
    if (bison!=null && context!=null)
    {
      List<String> bl=new ArrayList<String>();
      bl.add(bison);
      m_curr.bisons.put(context, bl);
    }
  }
  
  private void handleContextStart(Attributes attrs) throws SAXException
  {
    String context=SaxUtil.getMandatoryAttr("name", attrs, m_loc);
    m_currBisons=new ArrayList<String>();
    m_curr.bisons.put(context, m_currBisons);
  }
  
}